; $Id: obra.nsi 109 2010-11-12 02:09:31Z boumenot $
;
; -- ChangeLog
; cb /23 Sep 2010/: initial version

; ------------------------------------------------
; -- Includes

!include "FileFunc.nsh"
!include "MUI2.nsh"


; ------------------------------------------------
; -- Defines

!define PRODUCT_ID "a04aebfc-9bce-419b-aa74-29b170c3b65b"
!define PRODUCT_MAJOR_VERSION 0
!define PRODUCT_MINOR_VERSION 1
!define PRODUCT_NAME "Obra"
!define PRODUCT_PUBLISHER "Christopher Boumenot"
!define PRODUCT_REGISTRY_KEY "HKLM"
!define PRODUCT_VERSION "0.0.1"
!define PRODUCT_WEB_SITE "http://svn.divebomb.org/Obra"

!define ARP "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define INSTALLUTIL "$WINDIR\Microsoft.NET\Framework\v2.0.50727\installutil.exe"
!define POWERSHELL "$WINDIR\system32\WindowsPowerShell\v1.0\powershell.exe"

; ------------------------------------------------
; -- Defaults

CrcCheck              on
InstallDir            "$PROGRAMFILES\Obra"
Name                  "${PRODUCT_NAME}"
OutFile               "obra-${PRODUCT_VERSION}.exe"
RequestExecutionLevel admin
SetCompressor /SOLID  lzma
; SetCompress off

; ------------------------------------------------
; -- Pages 

;; Install 

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "..\LICENSE.txt"
; !insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

;; Uninstall

!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH

; ------------------------------------------------
; -- Languages

!insertmacro MUI_LANGUAGE "English"

; ------------------------------------------------
; -- Sections

Section "Obra" Obra
  SetOutPath "$INSTDIR"
  ; SetOverWrite IfNewer

  WriteUninstaller "$INSTDIR\Uninstall.exe"

  WriteRegStr   ${PRODUCT_REGISTRY_KEY} "Software\Obra" "" $INSTDIR

  ; WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "DisplayIcon" ""
  WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "DisplayName" "Obra - SmugMug Shell"
  WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "DisplayVersion" "${PRODUCT_VERSION}"
  ; WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "InstallLocation" "$INSTDIR"
  WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "MajorVersion" "${PRODUCT_MAJOR_VERSION}"
  WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "MinorVersion" "${PRODUCT_MINOR_VERSION}"
  WriteRegDWORD ${PRODUCT_REGISTRY_KEY} "${ARP}" "NoModify" 1
  WriteRegDWORD ${PRODUCT_REGISTRY_KEY} "${ARP}" "NoRepair" 1
  WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "ProductID" "${PRODUCT_ID}"
  WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "Publisher" "${PRODUCT_PUBLISHER}"
  ; WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "RegOwner" ""
  ; WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "RegCompany" ""
  WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "QuietUninstallString" "$\"$INSTDIR\uninstall.exe$\" /S"
  WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "UninstallString" "$\"$INSTDIR\uninstall.exe$\""
  WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  ; WriteRegStr   ${PRODUCT_REGISTRY_KEY} "${ARP}" "URLUpdateInfo" "${PRODUCT_WEB_SITE}"

  File "..\LICENSE.txt"

  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Microsoft.Practices.EnterpriseLibrary.Common.dll"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Microsoft.Practices.EnterpriseLibrary.Logging.dll"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Microsoft.Practices.ObjectBuilder2.dll"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Microsoft.Practices.Unity.dll"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Obra.Api.dll"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Obra.Api.pdb"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Obra.Common.dll"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Obra.Common.pdb"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Obra.PowerShell.SmugMug.dll"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Obra.PowerShell.SmugMug.pdb"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Obra.SmugMug.Api.dll"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\Obra.SmugMug.Api.pdb"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\SmugMug.psc1"
  File "..\src\Obra.PowerShell.SmugMug\bin\Debug\SmugMug.format.ps1xml"

  nsExec::ExecToLog '"${INSTALLUTIL}" "$INSTDIR\Obra.PowerShell.SmugMug.dll"'

  CreateDirectory "$SMPROGRAMS\Obra"
  CreateShortCut "$SMPROGRAMS\Obra\PowerShell.lnk" "${POWERSHELL}" '-PSConsoleFile "$INSTDIR\SmugMug.psc1"' "${POWERSHELL}" 0
  CreateShortCut "$SMPROGRAMS\Obra\Uninstall Obra.lnk" "$INSTDIR\Uninstall.exe"
SectionEnd

Section "Uninstall"
  nsExec::ExecToLog '"${INSTALLUTIL}" /uninstall "$INSTDIR\Obra.PowerShell.SmugMug.dll"'

  Delete "$INSTDIR\Uninstall.exe"
 
  Delete "$INSTDIR\LICENSE.txt"

  Delete "$INSTDIR\InstallUtil.InstallLog"

  Delete "$INSTDIR\app.config"
  Delete "$INSTDIR\Microsoft.Practices.EnterpriseLibrary.Common.dll"
  Delete "$INSTDIR\Microsoft.Practices.EnterpriseLibrary.Logging.dll"
  Delete "$INSTDIR\Microsoft.Practices.ObjectBuilder2.dll"
  Delete "$INSTDIR\Microsoft.Practices.Unity.dll"
  Delete "$INSTDIR\Obra.Api.dll"
  Delete "$INSTDIR\Obra.Api.pdb"
  Delete "$INSTDIR\Obra.Common.dll"
  Delete "$INSTDIR\Obra.Common.pdb"
  Delete "$INSTDIR\Obra.PowerShell.SmugMug.dll"
  Delete "$INSTDIR\Obra.PowerShell.SmugMug.InstallLog"
  Delete "$INSTDIR\Obra.PowerShell.SmugMug.InstallState"
  Delete "$INSTDIR\Obra.PowerShell.SmugMug.pdb"
  Delete "$INSTDIR\Obra.SmugMug.Api.dll"
  Delete "$INSTDIR\Obra.SmugMug.Api.pdb"
  Delete "$INSTDIR\SmugMug.psc1"
  Delete "$INSTDIR\SmugMug.format.ps1xml"

  RMDir "$INSTDIR"
  RMDir /r "$SMPROGRAMS\Obra"

  DeleteRegKey ${PRODUCT_REGISTRY_KEY} "${ARP}"
  DeleteRegKey ${PRODUCT_REGISTRY_KEY} "Software\Obra"

  DeleteRegKey HKCU "Software\${PRODUCT_NAME}"
  DeleteRegKey HKLM "Software\${PRODUCT_NAME}"
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
SectionEnd

; ------------------------------------------------
; -- Functions

Function .onInit
  ReadRegStr $0 ${PRODUCT_REGISTRY_KEY} "Software\Obra" ""
  StrCmp $0 "" obraNotInstalled
    MessageBox MB_ICONEXCLAMATION|MB_OK "Obra is already installed.  Please uninstall before continuing."
    Abort
  obraNotInstalled:

  ; Verify .NET 3.5 SP1 is installed
  ReadRegDWORD $0 ${PRODUCT_REGISTRY_KEY} "Software\Microsoft\NET Framework Setup\NDP\v3.5" "Install"  
  ReadRegDWORD $1 ${PRODUCT_REGISTRY_KEY} "Software\Microsoft\NET Framework Setup\NDP\v3.5" "SP"  

  IntCmp $0 1 isInstalled isNotInstalled isInstalled
  isInstalled:
    IntCmp $1 1 done isNotInstalled done
  isNotInstalled:
    MessageBox MB_ICONEXCLAMATION|MB_OK "Obra requires .NET v3.5 SP1!"
    Abort
  done:
    ; All requirements are installed, so begin installation
FunctionEnd

; Function .onInstSuccess
;   MessageBox MB_YESNO "View the readme?" IDNO SkipReadme
;     Exec 'notepad.exe "$INSTDIR\LICENSE.txt"'
;   SkipReadme:
;     ;         
; FunctionEnd